package com.kestrel.space.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.kestrel.space.common.model.User;
import com.kestrel.space.common.utils.BizException;
import com.kestrel.space.common.utils.Generator;
import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.core.dto.BlogDTO;
import com.kestrel.space.core.mapper.BlogMapper;
import com.kestrel.space.core.model.Blog;
import com.kestrel.space.core.model.Comment;
import com.kestrel.space.core.model.FileMappingOrg;
import com.kestrel.space.core.model.MyFile;
import com.kestrel.space.core.query.BlogQuery;
import com.kestrel.space.core.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author yebolin
 * @date 2024/8/23
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private FileMappingOrgService fileMappingOrgService;

    @Override
    public List<Blog> listContent(BlogQuery query, LargePage page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }
        Blog blog = BeanUtil.copyProperties(query, Blog.class);
        return blogMapper.select(blog);
    }

    @Override
    public String saveOrUpdate(Blog blog) {
        String message = "插入成功";
        if (blog.getId() == null) {
            blog.setId(Generator.generateId());
            blogMapper.insert(blog);
        } else {
            message = "更新成功";
            blogMapper.updateByPrimaryKeySelective(blog);
        }
        return message;
    }

    @Override
    public String batchSaveOrUpdate(List<Blog> blogList) {
        String message = "批量插入成功";
        if (CollectionUtils.isEmpty(blogList)) {
            return "集合为空";
        }
        if(blogList.get(0).getId() == null){
            for (Blog blog : blogList) {
                blog.setId(Generator.generateId());
                blogMapper.insert(blog);
            }
        } else {
            message = "批量更新成功";
            for (Blog blog : blogList) {
                blog.setId(Generator.generateId());
                blogMapper.updateByPrimaryKey(blog);
            }
        }
        return message;
    }

    @Override
    public String editBlogs(MultipartFile file, Long id) {
        MyFile oldFile = fileService.selectFileById(id);
        MyFile newFile = BeanUtil.copyProperties(oldFile, MyFile.class);
        fileService.saveOrUpdate(newFile);
        return "文件更替成功";
    }

    @Override
    public List<Blog> listPublicBlogs(BlogQuery query) {
        // 设置搜索公网博客
        query.setExposeLevel(1);
        return listContent(query, new LargePage(query.getPage(), query.getRows()));
    }

    @Override
    public List<Blog> listOrganizationBlogs(BlogQuery query) {
        // 设置搜索组织博客
        query.setExposeLevel(2);
        return blogMapper.listOrganizationBlogs(query);
    }

    @Override
    public String allocate(Long pdfId, Long organizationId, boolean stage) {
        FileMappingOrg fileMappingOrg = new FileMappingOrg();
        fileMappingOrg.setFileId(pdfId);
        fileMappingOrg.setOrganizationId(organizationId);
        fileMappingOrg.setIfDeleted(stage);
        return fileMappingOrgService.saveOrUpdate(fileMappingOrg);
    }

    @Override
    public List<Long> listMyBlogsInOrg(User user, Long organizationId) {
        return blogMapper.listMyBlogsInOrg(user.getId(),organizationId);
    }

    @Override
    public String downloadBlog(Long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            fileService.downloadFile(id, request,response);
        } catch (BizException e){
            return "下载失败";
        }
        return "下载成功";
    }

    @Override
    public String deleteBlog(Long id) {
        Blog blog = new Blog();
        blog.setId(id);
        blog.setIfDeleted(true);
        blog.setUpdateTime(new Date());
        Example example = new Example(Blog.class);
        example.createCriteria().andEqualTo("id",id);
        blogMapper.updateByExample(blog,example);
        return "删除博客成功";
    }

    @Override
    public BlogDTO showBlogsDetail(Long id) {
        Blog blog = (Blog) blogMapper.selectByExample(id);
        BlogDTO blogDTO = BeanUtil.copyProperties(blog, BlogDTO.class);
        MyFile pdf = fileService.selectFileById(blog.getPdfId());
        MyFile docx = fileService.selectFileById(blog.getDocxId());
        blogDTO.setPdf(pdf);
        blogDTO.setDocx(docx);
        return blogDTO;
    }
}
